﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2/13/2011
 * Time: 4:24 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Data.SQLite;
using FileSystemDatabase;
using System.Data;

namespace TomatoSynchronizer.Forms
{
	/// <summary>
	/// Description of SiteConfigForm.
	/// </summary>
	public partial class SiteConfigForm : Form
	{
		private HtmlWeb client;
		private JsonDatabase db;
		private Site[] _sites;
		
		public SiteConfigForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			db = new JsonDatabase("sites");
			client = new HtmlWeb();
			_sites = LoadSites();						
		}
		
		private Site[] LoadSites()
		{
			DataTable dtSites = new DataTable("sites");
			
			DataColumn colName = new DataColumn("Name",typeof(string));
			dtSites.Columns.Add(colName);

			DataColumn colUrl = new DataColumn("Url",typeof(string));
			dtSites.Columns.Add(colUrl);
			
			Site[] sites  = db.Read<Site>();
			
			dtSites.Rows.Clear();
			
			foreach(Site site in sites)
			{
				DataRow drSite = dtSites.NewRow();
				drSite["Name"] = site.Name;
				drSite["Url"] = site.Url;
				dtSites.Rows.Add(drSite);								
			}
			
			dataGridView1.DataSource = dtSites;
			
			return sites;
		}
		
		void BtnTestConnectionClick(object sender, EventArgs e)
		{
			Site site = new Site(txtNomSite.Text,txtUrl.Text,txtUser.Text,txtPass.Text);
			
			if(site.Login(client))
			{
				MessageBox.Show("OK");
			}
			else
			{
				MessageBox.Show("Echec");
			}
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			JsonDatabase sites = new JsonDatabase("sites");
			
			Site site = new Site(txtNomSite.Text,txtUrl.Text,txtUser.Text,txtPass.Text);
			
			sites.Write<Site>(site,txtNomSite.Text);
			MessageBox.Show("OK");
			
			LoadSites();
		}
		
		void BtnCloseClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void DataGridView1RowEnter(object sender, DataGridViewCellEventArgs e)
		{
//			MessageBox.Show("RowIndex :" + e.RowIndex + "<br>ColumnIndex : " +  e.ColumnIndex);
			int i = 0;
			foreach(DataGridViewRow dr in dataGridView1.Rows)
			{
				if(i == e.RowIndex && i < _sites.Length)
				{
					string name = dr.Cells[1].Value.ToString();
					
					foreach(Site site in _sites)
					{
						if(site.Name == name)
						{
							txtNomSite.Text = site.Name;
							txtPass.Text = site.Pass;
							txtUrl.Text = site.Url;
							txtUser.Text = site.User;
						}
					}			
				}	
				
				i = i + 1;
			}
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{			
			int i = 0;
			foreach(DataGridViewRow dr in dataGridView1.Rows)
			{				
				if(dr.Cells[0].Selected)
				{
					string name = dr.Cells[1].Value.ToString();
					
					foreach(Site site in _sites)
					{
						if(site.Name == name)
						{
							db.Delete<Site>(site.Name);
							i = i + 1;
						}
					}					
				}								
			}
			
			MessageBox.Show(i + " sites supprimes avec succes !!!");
			_sites = LoadSites();
		}
	}
}
